require 'rubygems'
require 'sinatra'
require 'json'
require 'sqlite3'
require 'logger'

configure do
  LOGGER = Logger.new('sinatra.log')
  DB = SQLite3::Database.new("sankofa.db")
  DB.results_as_hash = true
end

get '/' do
    content_type :json
    response['Access-Control-Allow-Origin'] = 'http://localhost'
   { :key1 => 'value1', :key2 => 'value2' }.to_json
end

post '/volunteers' do
    DB.execute("INSERT INTO volunteers (vlastname, vfirstname, vaddress, vage, vemail, vgender, vrace, vphonenumber, vwebsite) VALUES (?,?,?,?,?,?,?,?,?)",params[:vlastname],params[:vfirstname], params[:vaddress], params[:vage], params[:vemail], params[:vgender], params[:vrace], params[:vphonenumber], params[:vwebsite])
    response['Access-Control-Allow-Origin'] = 'http://localhost'
end
post '/volunteers/update' do
    DB.excute("Update volunteers set vfirstname=? where vid=?",params[:vfirstname],params[:vid])
    response['Access-Control-Allow-Origin'] = 'http://localhost'
end
get '/volunteers' do
    what = DB.execute("SELECT * FROM volunteers")
    response['Access-Control-Allow-Origin'] = 'http://localhost'
    what.to_json 
end

get '/events' do
    what = DB.execute("SELECT * FROM events")
    response['Access-Control-Allow-Origin'] = 'http://localhost'
    what.to_json 
end

post '/events' do
    DB.execute("INSERT INTO events (ename, ebegindate, eenddate, edescription) VALUES (?,?,?,?)",params[:ename],params[:ebegindate],params[:eenddate],params[:edescription])
    response['Access-Control-Allow-Origin'] = 'http://localhost'
end

get '/EventVolunteer/:id' do 
    what = DB.execute("SELECT DISTINCT vlastname, vfirstname FROM volunteers, EventVolunteer, events WHERE volunteers.vid = EventVolunteer.vid and EventVolunteer.eid = #{params[:id]}")
     response['Access-Control-Allow-Origin'] = 'http://localhost'
     what.to_json
end

post '/EventVolunteer' do 
    DB.execute("INSERT INTO EventVolunteer (eid, vid, ehours, edescription) VALUES (?,?,?,?)",params[:eid],params[:vid],params[:ehours],params[:edescription])
    response['Access-Control-Allow-Origin'] = 'http://localhost'
end



get '/plants' do
    what = DB.execute("SELECT * FROM batchofplants")
    response['Access-Control-Allow-Origin'] = 'http://localhost'
    what.to_json 
end

post '/plants' do
    what = DB.execute("INSERT INTO batchofplants (boptypeofplants, dateplanted, numberplanted) VALUES (?,?,?)",params[:boptypeofplants],params[:dateplanted],params[:numberplanted])
    response['Access-Control-Allow-Origin'] = 'http://localhost'
end

get '/plants/:plantname' do
    what = DB.execute("SELECT * FROM plants")
    response['Access-Control-Allow-Origin'] = 'http://localhost'
    what.to_json 
end


get '/volunteer/:volunteername' do
#"SELECT DISTINCT vlastname, vfirstname FROM volunteers, EventVolunteer, events WHERE volunteers.vid = EventVolunteer.vid and EventVolunteer.eid = events.eid"
end

get '/events/:eventname/' do

end

get '/events/search?q=:text&type=:type' do
end

get'/volunteer/search?q=:text&type=:type' do
end

get'/plants/search?q=:text&type=:type' do
end

